package com.yanqu.road.dao.impl.activity.climbtower;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.climbtower.ClimbTowerDrawNoticeData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ClimbTowerDrawNoticeDataDaoImpl extends TempDataObjectDao<ClimbTowerDrawNoticeData> {

    public static String getClimbTowerDrawNoticeDataSql = "select * from `t_c_climb_tower_draw_notice` where `activityId` in (?)";
    public Map<Integer, List<ClimbTowerDrawNoticeData>> getClimbTowerDrawNoticeData(List<Integer> activityIdList) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<ClimbTowerDrawNoticeData>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getClimbTowerDrawNoticeDataSql);
            int i = 1;
            call.setString(i++, StringUtils.listToString(activityIdList, ","));
            rs = call.executeQuery();
            while (rs.next()) {
                ClimbTowerDrawNoticeData data = parse(rs);
                if(!result.containsKey(data.getActivityId())){
                    result.put(data.getActivityId(), new ArrayList<>());
                }
                result.get(data.getActivityId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertClimbTowerDrawNoticeDataSql = "insert into `t_c_climb_tower_draw_notice`(activityId,userId,serverId,nickName,reward,getTime,param) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(ClimbTowerDrawNoticeData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getNickName()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getParam()));
        if (getDb().executeNoneQuery(insertClimbTowerDrawNoticeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateClimbTowerDrawNoticeDataSql = "update `t_c_climb_tower_draw_notice` set userId=?,serverId=?,nickName=?,reward=?,getTime=?,param=? where activityId=?";

    @Override
    protected boolean updateData(ClimbTowerDrawNoticeData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getNickName()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getParam()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateClimbTowerDrawNoticeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected ClimbTowerDrawNoticeData parseData(ResultSet rs) throws SQLException {
        ClimbTowerDrawNoticeData climbTowerDrawNoticeData = new ClimbTowerDrawNoticeData();
        climbTowerDrawNoticeData.setActivityId(rs.getInt("activityId"));
        climbTowerDrawNoticeData.setUserId(rs.getLong("userId"));
        climbTowerDrawNoticeData.setServerId(rs.getLong("serverId"));
        climbTowerDrawNoticeData.setNickName(rs.getString("nickName"));
        climbTowerDrawNoticeData.setReward(rs.getString("reward"));
        climbTowerDrawNoticeData.setGetTime(rs.getLong("getTime"));
        climbTowerDrawNoticeData.setParam(rs.getString("param"));

        return climbTowerDrawNoticeData;
    }
}
